package com.hp.ssmp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.ssmp.entity.TUser;
import com.hp.ssmp.service.UserService;
import com.hp.ssmp.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 分页查询角色列表
     * @param currentPage
     * @param pageSize
     * @param sex
     * @return userList
     */
    @GetMapping("/list")
    public R<Page<TUser>> page(Integer currentPage, Integer pageSize, Long sex) {
        Page<TUser> userPage = userService.getUserPage(currentPage, pageSize, sex);
        return R.success(userPage);
    }

    /**
     * 冻结与启用用户
     * @param user
     * @return
     */
    @GetMapping("/status")
    public R status(TUser user) {
        boolean result = userService.updateById(user);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }
    @GetMapping("/getById")
    public R<TUser> getById(Long userid) {
        TUser user = userService.getById(userid);
        user.setSalt(null);
        user.setPassword(null);
        return R.success(user);
    }

    @PostMapping("/add")
    public R add(TUser user) {
        boolean result = userService.addUser(user);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    @GetMapping("/del")
    public R del(Long userid) {
        boolean result = userService.removeById(userid);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    @PostMapping("/update")
    public R update(TUser user) {
        boolean result = userService.updateById(user);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }
    @PostMapping("/updpwd")
    public R updpwd(Long userid,String password,String newpassword) {
        boolean result = userService.updatePwd(userid,password,newpassword);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }


}
